/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.service.SysGeneratorService;
import io.renren.utils.PageUtils;
import io.renren.utils.Query;
import io.renren.utils.R;
import io.renren.utils.RRException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 * 
 * @author Mark
 */
@Controller
@RequestMapping("/sys/generator")
public class SysGeneratorController {
	@Autowired
	private SysGeneratorService sysGeneratorService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils pageUtil = sysGeneratorService.queryList(new Query(params));

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(String tables, String modelType, HttpServletResponse response) throws IOException {
		if (StringUtils.isBlank(modelType)||"null".equals(modelType)) {
			throw new RRException("你未选择模板类型！！！");
		}
		byte[] data = sysGeneratorService.generatorCode(tables.split(","), modelType);
		String fileName = tables.replaceAll(",", "-");
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".zip");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IOUtils.write(data, response.getOutputStream());
	}
	
	
	
	@PostMapping("/jsonCreateJavaEntity")
	public void jsonCreateJavaEntity(@RequestBody JSONObject json,HttpServletResponse response) throws IOException{
		Map<String,Object> jsonMap=new HashMap<String,Object>();
		List<String> jsonKey=new ArrayList<>();
		for (Map.Entry<String, Object> entry : json.entrySet()) {
			jsonMap.put(entry.getKey(), entry.getValue());
			jsonKey.add(entry.getKey());
        }
		
		byte[] data = sysGeneratorService.jsonCreateJavaEntityFile(jsonMap,jsonKey);
		String fileName = "json代码";
		response.reset();
		response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".zip");
		response.addHeader("Content-Length", "" + data.length);
		response.setContentType("application/octet-stream; charset=UTF-8");
		IOUtils.write(data, response.getOutputStream());
	
	}
}
